package dome6Test;

import java.util.*;
import java.util.stream.Collectors;

public class Room {
    //准备好54张牌，放到房间中，发牌，发完牌，游戏结束。
    //定义集合容器用来存储牌
    private List<Card> allcards = new ArrayList<>();//牌
    //初始化五十四张牌进去
    {
        String[] sizes = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        String[] colors = {"♥","♠","♣","♦"};
        int num=0;
        for (String size : sizes) {
            num++;
            //循环遍历花色
            for (String color : colors) {
                allcards.add(new Card(size,color,num));
            }
        }
        allcards.add(new Card("","🃏",num+1));//添加小王
        allcards.add(new Card("","👲",num+2));//添加大王
        System.out.println("新牌是"+allcards);
    }

    public void start() {
        //洗牌
        Collections.shuffle(allcards);
        System.out.println("洗牌后"+allcards);
        //发牌
        //1.定义三个集合，用来存储玩家的牌，底牌，用map集合来存储，key：玩家，value：牌
        Map<String ,List<Card>> players = new HashMap<>();
        List<Card> player1 = new ArrayList<>();
        players.put("p1",player1);
        List<Card> player2 = new ArrayList<>();
        players.put("p2",player2);
        List<Card> player3 = new ArrayList<>();
        players.put("p3",player3);
        List<Card> bottom = new ArrayList<>();
        players.put("底牌",bottom);
        for (int i = 0; i <allcards.size(); i++) {
            Card card = allcards.get(i);
            if(i>=51){
                //底牌

                bottom.add(card);

            }else if(i%3==0){
                //玩家1
                player1.add(card);
            }else if(i%3==1){
                //玩家2
                player2.add(card);
            }else{
                //玩家3
                player3.add(card);
            }

        }
        player1.addAll(bottom);
        //对排排序，根据牌大小进行排序
        sortAllCards(player1);
        sortAllCards(player2);
        sortAllCards(player3);



        //看牌
        for (Map.Entry<String, List<Card>> entry : players.entrySet()) {
            String key = entry.getKey();
            List<Card> value = entry.getValue();

            System.out.println(key+"的牌是"+value);
        }

    }

    private void sortAllCards(List<Card> player) {
//        player.sort(new Comparator<Card>() {
//            @Override
//            public int compare(Card o1, Card o2) {
//                return o1.getNum()-o2.getNum();
//            }
//        }
//        );
        player.sort(Comparator.comparingInt(Card::getNum));
    }
}
